<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-10-10 13:45
 *
 * 项目：rm  -  $  - genFormController.php
 *
 * 作者：liwei 
 */

namespace modules\levmodules\helpers;

use Lev;
use lev\base\Modelv;
use lev\helpers\dbHelper;
use lev\helpers\ModulesHelper;
use lev\helpers\SettingsHelper;

!defined('INLEV') && exit('Access Denied LEV');


class generateFormHelper extends generateHelper
{


    /**
     * @param $iden
     * @param $tableName
     * @param $classify
     * @param string $formName
     * @param array $replaces
     * @param bool $forceGen
     * @param null $_inputsForm
     * @param array $unFiles
     * @return array
     */
    public static function generates($iden, $tableName, $classify, $formName = '', $replaces = [], $forceGen = false, $_inputsForm = null, $unFiles = [])
    {
        $formName = $formName ?: $classify;
        if ($errMsg = static::isErrorIden($formName)) {
            static::$tips .= '<tips>'.$formName.'表单名称不合法：'.$errMsg['message'].'</tips> <br>';
            return $errMsg;
        }
        $cols = dbHelper::existsTable($tableName) ? dbHelper::getColumnsComment($tableName) : [];
        $_inputsForm === null &&
        $_inputsForm = SettingsHelper::getModuleTabSettings($iden, $classify);
        if (empty($_inputsForm)) {
            static::$tips .= '<tips>'.$classify.'表单字段空</tips> <br>';
            return Lev::responseMsg(-10101, '表单字段空');
        }
        $inputsForm = $setupDescForm = [];
        foreach ($_inputsForm as $v) {
            //$v['fdid'] = $v['id'];
            unset($v['id'], $v['uptime'], $v['addtime']);
            $inputsForm[$v['inputname']] = $v;
            $setupDescForm[$v['status']] = '第'.$v['status'].'步';
        }

        $replaces = parent::replaceVars($iden, $replaces);
        $replaces['Form_myname']         = Modelv::formRoute($formName);
        $replaces['{{$tableName}}']      = Modelv::preTableName($tableName);;
        $replaces['"{{allColumns}}"']    = static::varExportv($cols);
        $replaces['"{{inputsForm}}"']    = static::varExportv($inputsForm);
        $replaces['"{{setupDescForm}}"'] = static::varExportv($setupDescForm);

        return static::doGenerates($iden, $replaces, $forceGen, $unFiles);
    }

    /**
     * @param $iden
     * @param array $replaces
     * @param bool $forceGen
     * @param array $unFiles
     * @return array
     */
    public static function doGenerates($iden, $replaces = [], $forceGen = false, $unFiles = []) {

        $ckModule = static::isExistIden($idenDir = ModulesHelper::getIdenDir($iden));
        if (!$ckModule) {
            static::$tips .= '请先创建模块 ';
            return Lev::responseMsg(-10111, '请先创建模块');
        }
        $moduleIdenDir = Lev::$aliases['@modules'] . '/' . $idenDir;
        $ckForm = $moduleIdenDir . '/table/form/' . $replaces['Form_myname'] . '/' . $replaces['Form_myname'] . '.php';
        if (!$forceGen && is_file($ckForm)) {
            static::$tips .= ' <tips>表单模型已经创建过了！</tips>'.basename($ckForm).'<br>';
            return Lev::responseMsg(-10111, '控制器已经创建过了！'.$ckForm);
        }
        $generateDir = dirname(__DIR__) . '/_generates/_form/_levcc';
        $unFiles[$ckForm] = 1;
        $unFiles[$moduleIdenDir . '/controllers/' . $replaces['Form_myname'] . 'Controller.php'] = 1;
        static::doGenerateFiles($generateDir, $moduleIdenDir, array_keys($replaces), array_values($replaces), $unFiles);
        static::$tips .= ' <span class="color-green">表单模型创建成功：'.basename($ckForm).'</span><br>';
        return Lev::responseMsg(1, '', ['files'=>static::$generateFiles]);
    }

    public static function createAdminForm($iden, $forms)
    {
        foreach ($forms as $v) {
            $replaces = [];
            static::generates($iden, $v['tableName'], $v['formInputs'], $v['formName'], $replaces, !$v['forceGen']);
        }
        static::$tips && Lev::setNotices(static::$tips, true);
    }

}